import { createRouter, createWebHistory } from 'vue-router';

const routes = [
    {
        path: '/',
        redirect: '/back'  // 从根路径重定向到 /login
    },
    {
        path: '/login',
        component: () => import('@/components/CxkLogin.vue')
    },
    {
        path: '/back',
        component: () => import('@/components/CxkQun.vue'),
        children: [
            {
                path: '', // 默认子路由
                alias: '/back/home', // 别名
                components: {
                    left: () => import('@/components/home/AllChart.vue'),
                    right: () => import('@/components/home/LinksData.vue')
                }
            },
            {
                path: 'table', // 子路由的路径不需要以 / 开头
                components: {
                    left: () => import('@/components/table/UserTable.vue'),
                    right: () => import('@/components/table/TablePanel.vue')
                }
            },
            {
                path: 'links', // 子路由的路径不需要以 / 开头
                components: {
                    left: () => import('@/components/links/ShortLinks.vue'),
                }
            },
            {
                path: 'group', // 子路由的路径不需要以 / 开头
                components: {
                    left: () => import('@/components/group/GroupShort.vue'),
                    right: () => import('@/components/group/GroupTable.vue'),
                }
            }
        ]
    }
];


const router = createRouter({
    history: createWebHistory(),
    routes,
});


router.beforeEach((to,from,next) => {

    // 排除登录页面
    if (to.path !== '/login') {
        // 获取 Cookie 中的认证信息
        const auth = $cookies.get('frontauthtoken');
        // 如果认证信息存在且有效，允许导航到目标路由
        if (auth === 'qp9iHXwRNAP4VkjfHV3P') {
            next();
        } else {
            // 否则重定向到登录页面
            next('/login');
        }
    } else {
        // 如果目标路由是登录页面，直接放行
        next();
    }
});

export default router;